package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ArticleVo
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.jiCengDangJian.adapter.PartyServiceMainListAdapter
import com.open.party.cloud.view.login.LoginActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import com.sinothk.widget.loadingRecyclerView.extend.LoadingRecycleViewHeader
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ZyzServiceMainActivity : AppRecycleViewTitleBaseActivity<ArticleVo>(), OnSuperListener<ArticleEntity>, View.OnClickListener {

    var viewModel: ArticleViewModel? = null
    private var adapter: PartyServiceMainListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider
    }

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)

        refreshData()
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("党员服务")
        viewModel = ArticleViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)
        loadingRecyclerView.addHeaderView(createHeaderView())

        adapter = PartyServiceMainListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<ArticleVo>) {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean("JCDJ_HDJB", "")
        pageReq.data = vo
        viewModel!!.getArticleListData(pageReq)
    }

    override fun onClick(position: Int, itemData: ArticleEntity, flag: String) {
        if (itemData.isExternalLink && XUtils.string().isNotEmpty(itemData.source)) {
//            val bundle = Bundle()
//            bundle.putString("url", itemData.source)
//            CommWebDetailsActivity.start(this@PartyServiceMainActivity, bundle)
        } else {
            XUtils.intent().openActivity(this@ZyzServiceMainActivity, ArticleDetailsActivity::class.java)
                .putStringExtra("id", itemData.id)
                .start()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ArticleEntity>>) {

        if ("getArticleListData" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingRecyclerView.setLoadingMoreEnabled(false)
                adapter!!.setData(ArrayList<ArticleEntity>())
                loadingTipView.showContent()
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<ArticleEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingRecyclerView.setLoadingMoreEnabled(false)
                adapter!!.setData(ArrayList<ArticleEntity>())
                loadingTipView.showContent()

            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用

        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    /**
     * 创建HeaderView
     */
    private var item01: LinearLayout? = null
    private var item02: LinearLayout? = null

    //    private var item03: LinearLayout? = null
    private var item04: LinearLayout? = null

    private fun createHeaderView(): View {
        val headerView: View = LoadingRecycleViewHeader.getViewByLayoutId(this, R.layout.zyz_main_header_view)
        item01 = headerView.findViewById(R.id.item01)
        item02 = headerView.findViewById(R.id.item02)
//        item03 = headerView.findViewById(R.id.item03)
        item04 = headerView.findViewById(R.id.item04)
//        val item05: LinearLayout = headerView.findViewById(R.id.item05)

        item01!!.setOnClickListener(this)
        item02!!.setOnClickListener(this)
//        item03!!.setOnClickListener(this)
        item04!!.setOnClickListener(this)

        return headerView
    }

    override fun onClick(v: View?) {
        if (!LocalCache.getAutoLogin()) {
            // 登录检测
            XUtils.intent().openActivity(this, LoginActivity::class.java).start()
            return
        }

        when (v) {
            item01 -> {
                XUtils.intent().openActivity(this@ZyzServiceMainActivity, ZyzTuanDuiListActivity::class.java).start()
            }
            item02 -> {
                XUtils.intent().openActivity(this@ZyzServiceMainActivity, ZyzHuoDongListActivity::class.java).start()
            }
//            item03 -> {
//                XUtils.intent().openActivity(this@ZyzMainActivity, ZyzBangWoListActivity::class.java).start()
//            }
            item04 -> {
                XUtils.intent().openActivity(this@ZyzServiceMainActivity, ZyzJiFenMainActivity::class.java).start()
            }
        }
    }
}